package com.yjhealth.libs.core.core.fragment;

import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.view.View;

import com.yjhealth.libs.core.R;
import com.yjhealth.libs.core.net.utils.NetworkErrorUtil;
import com.yjhealth.libs.core.toast.ToastUtil;
import com.yjhealth.libs.core.view.dialog.LoadingDialog;
import com.yjhealth.libs.core.view.loadview.LoadViewHelper;

public abstract class CoreLoadViewFragment extends CoreLazyFragment{

    protected LoadingDialog loadingDialog;
    protected LoadViewHelper loadViewHelper;
    
    private boolean isEmpty = true;

    protected abstract void onRefreshView();

    public boolean isEmpty() {
        return isEmpty;
    }

    public void setEmpty(boolean empty) {
        isEmpty = empty;
    }
    
    protected void intiLoadView(View mainView) {
        View base = mainView.findViewById(R.id.yjhealthCoreLoadLay);
        if (base != null) {
            loadViewHelper = new LoadViewHelper(this, base);
            loadViewHelper.setErrorClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRefreshView();
                }
            });
        }
    }


    //******************************** yjhealthCoreLoadLay *************************************************

    public boolean isViewReplace() {
        if (loadViewHelper == null) {
            return false;
        }
        return loadViewHelper.isRellace();
    }

    //***************** restore **************
    public void restoreView() {
        isEmpty = false;
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.restore();
    }

    //***************** loading **************
    public void showLoadingView() {
        showLoadingView(0, null, 0);
    }

    public void showLoadingView(int size) {
        showLoadingView(0, null, size);
    }

    public void showLoadingView(@DrawableRes int resourceId, String text) {
        showLoadingView(resourceId, text, 0);
    }

    public void showLoadingView(@DrawableRes int resourceId, String text, int size) {
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showLoading(resourceId, text, size);
    }

    //***************** empty **************
    public void showEmptyView() {
        showEmptyView(0, null, 0);
    }

    public void showEmptyView(int size) {
        showEmptyView(0, null, size);
    }

    public void showEmptyView(@DrawableRes int resourceId, String text) {
        showEmptyView(resourceId, text, 0);
    }

    public void showEmptyView(@DrawableRes int resourceId, String text, int size) {
        isEmpty = true;
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showEmpty(resourceId, text, size);
    }

    //***************** error 单图片 **************
    public void showErrorViewSimple(String errorType, String errorMsg) {
        showErrorViewSimple(errorType, errorMsg, 0, null, 0);
    }

    public void showErrorViewSimple(String errorType, String errorMsg, int size) {
        showErrorViewSimple(errorType, errorMsg, 0, null, size);
    }

    public void showErrorViewSimple(String errorType, String errorMsg, @DrawableRes int defRes, String defText) {
        showErrorViewSimple(errorType, errorMsg, defRes, defText, 0);
    }

    public void showErrorViewSimple(String errorType, String errorMsg, @DrawableRes int defRes, String defText, int size) {
        if (TextUtils.equals(errorType, NetworkErrorUtil.ERROR_TYPE_SHOW)) {
            ToastUtil.toast(errorMsg);
        }
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showErrorSimple(errorType, defRes, defText, size);
    }

    public void showErrorViewSimpleReplace(String errorType, String errorMsg, @DrawableRes int resourceId, String text) {
        showErrorViewSimpleReplace(errorType, errorMsg, resourceId, text, 0);
    }

    public void showErrorViewSimpleReplace(String errorType, String errorMsg, @DrawableRes int resourceId, String text, int size) {
        if (TextUtils.equals(errorType, NetworkErrorUtil.ERROR_TYPE_SHOW)) {
            ToastUtil.toast(errorMsg);
        }
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showErrorSimple(resourceId, text, size);
    }

    //***************** error 点击重试 **************
    public void showErrorView(String errorType, String errorMsg) {
        showErrorView(errorType, null, 0, null);
    }

    public void showErrorView(String errorType, String errorMsg, @DrawableRes int defRes, String defText) {
        if (TextUtils.equals(errorType, NetworkErrorUtil.ERROR_TYPE_SHOW)) {
            ToastUtil.toast(errorMsg);
        }
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showError(errorType, defRes, defText);
    }

    public void showErrorViewReplace(String errorType, String errorMsg, @DrawableRes int resourceId, String text) {
        if (TextUtils.equals(errorType, NetworkErrorUtil.ERROR_TYPE_SHOW)) {
            ToastUtil.toast(errorMsg);
        }
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showError(resourceId, text);
    }


    //***************** loadingDialog **************
    public void showLoadingDialog() {
        showLoadingDialog(0, null);
    }

    public void showLoadingDialog(@DrawableRes int resourceId, String msg) {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog.newInstance(resourceId, msg);
        }
        loadingDialog.show(activity, "loading");
    }

    public void dismissLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }
}
